﻿using UnityEngine;
using System.Collections.Generic;

public class Player : Character {

    public GameObject m_MPBar;
    public GameObject m_RageBar;
    private int m_MaxMP;
    private int m_MaxRage;

    public int CurMP { get; set; }
    public int MaxMP {
        get {
            return m_MaxMP;
        }
        set {
            m_MaxMP = value;
        }
    }
    public int CurRage { get; set; }
    public int MaxRage {
        get {
            return m_MaxRage;
        }
        set {
            m_MaxRage = value;
        }
    }

	public override void Init (Sys_Role data) {
        base.Init(data);
        CurMP = 0;
        CurRage = 0;
        MaxMP = Data.Mp;
        MaxRage = Data.Rage;
    }

    public void AddMp(float mp) {
        CurMP += Mathf.CeilToInt(mp);
        CurMP = CurMP > MaxMP ? MaxMP : CurMP;
        m_MPBar.GetComponent<ImageFillProgress>().AddPercent(mp);
    }

    public void AddRage(float rage)
    {
        CurRage += Mathf.CeilToInt(rage);
        CurRage = CurRage > MaxRage ? MaxRage : CurRage;
        m_RageBar.GetComponent<ImageFillProgress>().AddPercent(rage);
    }

    public void Attack(List<Star> allDestroyStarList,List<Star> starLink)
    {
        Damage damage = new Damage(m_StarBox.m_Girl);
        //先计算链接表中的，链接表中的要计算连消加成
        //忽略链接两种宝石的特殊宝石加成，所以循环中只认定只有一种宝石
        int count = 0;
        Sys_Star data = null;
        //记录下此次计算过的宝石
        List<Star> countedList = new List<Star>();
        for (int i = 0; i < starLink.Count; i++)
        {
            if (starLink[i].Data.Type == 1 || starLink[i].Data.Type == 2)
            {
                data = starLink[i].Data;
                count++;
                countedList.Add(starLink[i]);
            }
        }

        float addDamage = 0;
        float addMp = 0;
        float addRage = 0;
        print("连消加成系数 " + count);
        if (data.DamageRate != "0")
        {
            string[] value = data.DamageRate.Split(':');
            //1个宝石加成为0，所以要减掉1个加成系数
            addDamage = (float.Parse(value[0]) + float.Parse(value[1]) * (count - 1)) * count;
        }
        else if (data.MpRate != "0")
        {
            string[] value = data.MpRate.Split(':');
            addMp = (float.Parse(value[0]) + float.Parse(value[1]) * (count - 1)) * count;
        }
        else if (data.RageRate != "0")
        {
            string[] value = data.RageRate.Split(':');
            addRage = (float.Parse(value[0]) + float.Parse(value[1]) * (count - 1)) * count;
        }
        print("Linked damange : " + addDamage + " , mp : " + addMp + " , Rage : " + addRage);
        //先增加怒气或者能量
        AddMp(addMp);
        AddRage(addRage);


        if (addDamage != 0)
        {
            damage.AddDamage((Damage.DamageType)data.Id, addDamage);
        }

        //计算其他被毁宝石的分数
        for (int i = 0; i < allDestroyStarList.Count; i++)
        {
            Star star = allDestroyStarList[i];
            //之前没有计算过的宝石，才加入计算
            if (countedList.Contains(star))
                continue;
            //单独加分的额外宝石
            if (star.Data.Type == 1)
            {
                damage.AddDamage((Damage.DamageType)star.Data.Id, float.Parse(star.Data.RageRate.Split(':')[0]));
            }
            else if (star.Data.Type == 2)
            {
                if (star.Data.MpRate != "0")
                {
                    AddMp(float.Parse(star.Data.MpRate.Split(':')[0]));
                }
                else if (star.Data.RageRate != "0")
                {
                    AddRage(float.Parse(star.Data.RageRate.Split(':')[0]));
                }
            }
            else if (star.Data.Type == 3)
            {
                damage.AddDamage(Damage.DamageType.Chaos, float.Parse(star.Data.RageRate.Split(':')[0]));
            }
        }
        //记录怒气加成
        damage.AddRate = CurRage / 100.0f;
        damage.Active();
    }
}
